﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

using EFong.ServicesInterface;
using EFong.Contract;
using UIShare;

namespace EFong.View.Proxy
{
    public class BusinessServiceProxy : ClientBase<IBusinessService>, IBusinessService
    {
        #region Constructor

        public BusinessServiceProxy(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        { 
        }

        public BusinessServiceProxy(InstanceContext callbackInstance) :
            base(callbackInstance)
        {
        }

        public BusinessServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName) :
            base(callbackInstance, endpointConfigurationName)
        {
        }

        public BusinessServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public BusinessServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public BusinessServiceProxy(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) :
            base(callbackInstance, binding, remoteAddress)
        {
        }

        public BusinessServiceProxy(Binding binding, EndpointAddress remoteAddress) :
            base( binding, remoteAddress)
        {
        }

        private static BusinessServiceProxy _instance;
        public static BusinessServiceProxy GetInstance() 
        {
            if (_instance == null || _instance.State == CommunicationState.Faulted)
            {
                string url = string.Format("Http://{0}:{1}/{2}", ProxyMgt.IP, ProxyMgt.Port, "BusinessService");

                EndpointAddress epa = new EndpointAddress(url);

                WSHttpBinding binding = new WSHttpBinding();
                //binding.ReliableSession.Enabled = true;
                binding.Security.Mode = SecurityMode.None;
                binding.SendTimeout = new TimeSpan(0, 1, 30);
                binding.MaxReceivedMessageSize = 65536000;
                binding.ReaderQuotas.MaxArrayLength = 65536000;
                binding.ReaderQuotas.MaxStringContentLength = 65536000;

                _instance = new BusinessServiceProxy(binding, epa);
            }
            return _instance;
        }

        #endregion

        #region IBusinessService 成员

        public ServiceResult<string> NewFlightOrder(FlightOrder flightOrder)
        {
            var result = base.Channel.NewFlightOrder(flightOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> UpdateFlightOrder(FlightOrder flightOrder)
        {
            var result = base.Channel.UpdateFlightOrder(flightOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult VoidFlightOrder(FlightOrder flightOrder, string cause)
        {
            var result = base.Channel.VoidFlightOrder(flightOrder,cause);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult KeepFlightOrderDetail(FlightOrderDatail flightOrderDatail) 
        {
            var result = base.Channel.KeepFlightOrderDetail(flightOrderDatail);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> ChangeFlightOrder(FlightChange flightChange, FlightOrderDatail flightOrderDatail, decimal profit)
        {
            var result = base.Channel.ChangeFlightOrder(flightChange, flightOrderDatail, profit);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> ReturnFlightOrder(FlightReturn flightReturn, FlightOrderDatail flightOrderDatail, decimal profit)
        {
            var result = base.Channel.ReturnFlightOrder(flightReturn, flightOrderDatail, profit);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> NewTrainTicketOrder(TrainTicketOrder trainTicketOrder)
        {
            var result = base.Channel.NewTrainTicketOrder(trainTicketOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> UpdateTrainTicketOrder(TrainTicketOrder trainTicketOrder)
        {
            var result = base.Channel.UpdateTrainTicketOrder(trainTicketOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult VoidTrainTicketOrder(TrainTicketOrder trainTicketOrder, string cause)
        {
            var result = base.Channel.VoidTrainTicketOrder(trainTicketOrder,cause);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> ReturnTrainTicketOrder(Contract.TrainTicketReturn trainTicketReturn, Contract.TrainTicketOrderDetail trainTicketOrderDetail, decimal profit)
        {
            var result = base.Channel.ReturnTrainTicketOrder(trainTicketReturn, trainTicketOrderDetail, profit);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> NewHotelBooking(Contract.HotelBooking hotelBooking)
        {
            var result = base.Channel.NewHotelBooking(hotelBooking);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> UpdateHotelBooking(Contract.HotelBooking hotelBooking)
        {
            var result = base.Channel.UpdateHotelBooking(hotelBooking);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult VoidHotelBooking(Contract.HotelBooking hotelBooking, string cause)
        {
            var result = base.Channel.VoidHotelBooking(hotelBooking,cause);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> NewOtherBusiness(Contract.OtherBusiness otherBusiness)
        {
            var result = base.Channel.NewOtherBusiness(otherBusiness);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> UpdateOtherBusiness(Contract.OtherBusiness otherBusiness)
        {
            var result = base.Channel.UpdateOtherBusiness(otherBusiness);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult VoidOtherBusiness(Contract.OtherBusiness otherBusiness, string cause)
        {
            var result = base.Channel.VoidOtherBusiness(otherBusiness,cause);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult IntergralExchange(Contract.IntergralExchange intergralExchange)
        {
            var result = base.Channel.IntergralExchange(intergralExchange);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.IntergralExchange>> GetIntergralExchangeByMonth(DateTime month) 
        {
            var result = base.Channel.GetIntergralExchangeByMonth(month);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetAllFlightOrder()
        {
            var result = base.Channel.GetAllFlightOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByRef(string _ref)
        {
            var result = base.Channel.GetFlightOrderByRef(_ref);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByCustomer(Contract.RCustomer customer)
        {
            var result = base.Channel.GetFlightOrderByCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByOrderDate(DateTime startDate, DateTime endDate)
        {
            var result = base.Channel.GetFlightOrderByOrderDate(startDate,endDate);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByVendor(Contract.RVendor vendor)
        {
            var result = base.Channel.GetFlightOrderByVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByPassenger(string passenger)
        {
            var result = base.Channel.GetFlightOrderByPassenger(passenger);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByTicketNo(string ticketNo)
        {
            var result = base.Channel.GetFlightOrderByTicketNo(ticketNo);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByAirwaysOrderNo(string airwaysOrderNo)
        {
            var result = base.Channel.GetFlightOrderByAirwaysOrderNo(airwaysOrderNo);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RFlightOrder>> GetFlightOrderByWayType(string wayType)
        {
            var result = base.Channel.GetFlightOrderByWayType(wayType);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RFlightOrder>> GetFlightOrderByJourneyOrder(string journeyOrder) 
        {
            var result = base.Channel.GetFlightOrderByJourneyOrder(journeyOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RTrainTicketOrder>> GetAllTrainTicketOrder()
        {
            var result = base.Channel.GetAllTrainTicketOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RTrainTicketOrder>> GetTrainTicketOrderByRef(string _ref)
        {
            var result = base.Channel.GetTrainTicketOrderByRef(_ref);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RTrainTicketOrder>> GetTrainTicketOrderByCustomer(Contract.RCustomer customer)
        {
            var result = base.Channel.GetTrainTicketOrderByCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RTrainTicketOrder>> GetTrainTicketOrderByOrderDate(DateTime startDate, DateTime endDate)
        {
            var result = base.Channel.GetTrainTicketOrderByOrderDate(startDate,endDate);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<RTrainTicketOrder>> GetTrainTicketOrderByVendor(Contract.RVendor vendor)
        {
            var result = base.Channel.GetTrainTicketOrderByVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetAllHotelBooking()
        {
            var result = base.Channel.GetAllHotelBooking();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetHotelBookingByRef(string _ref)
        {
            var result = base.Channel.GetHotelBookingByRef(_ref);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetHotelBookingByGuestName(string guestName)
        {
            var result = base.Channel.GetHotelBookingByGuestName(guestName);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetHotelBookingByCustomer(Contract.RCustomer customer)
        {
            var result = base.Channel.GetHotelBookingByCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetHotelBookingByOrderDate(DateTime startDate, DateTime endDate)
        {
            var result = base.Channel.GetHotelBookingByOrderDate(startDate,endDate);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RHotelBooking>> GetHotelBookingByVendor(Contract.RVendor vendor)
        {
            var result = base.Channel.GetHotelBookingByVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetAllOtherBusiness()
        {
            var result = base.Channel.GetAllOtherBusiness();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetOtherBusinessByRef(string _ref)
        {
            var result = base.Channel.GetOtherBusinessByRef(_ref);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetOtherBusinessByCustomer(Contract.RCustomer customer)
        {
            var result = base.Channel.GetOtherBusinessByCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetOtherBusinessByOrderDate(DateTime startDate, DateTime endDate)
        {
            var result = base.Channel.GetOtherBusinessByOrderDate(startDate,endDate);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetOtherBusinessByVendor(Contract.RVendor vendor)
        {
            var result = base.Channel.GetOtherBusinessByVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.ROtherBusiness>> GetOtherBusinessByProject(Contract.Project project, SubProject subProject, string content)
        {
            var result = base.Channel.GetOtherBusinessByProject(project, subProject, content);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<FlightOrder> GetFlightOrderByID(Guid id)
        {
            var result = base.Channel.GetFlightOrderByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }



        public ServiceResult<TrainTicketOrder> GetTrainTicketOrderByID(Guid id)
        {
            var result = base.Channel.GetTrainTicketOrderByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<Contract.HotelBooking> GetHotelBookingByID(Guid id)
        {
            var result = base.Channel.GetHotelBookingByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<Contract.OtherBusiness> GetOtherBusinessByID(Guid id)
        {
            var result = base.Channel.GetOtherBusinessByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RFlightOrder>> GetFlightOrderByIsKeep(bool isKeep)
        {
            var result = base.Channel.GetFlightOrderByIsKeep(isKeep);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        #endregion

    }
}
